<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>客户采纳产品管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		var buyProductId = "";
		$(document).ready(function() {			
			$("#contentTable tbody tr").click(function(){
				$("#contentTable tbody tr").removeClass("select_active");
				$(this).addClass("select_active");
			});
			$("#contentTable tbody tr:first-child").click();
		});
		
	    function buyProduct(id){
	    	buyProductId = id;
	    	var name = $("#contentTable tbody tr.select_active .name").html();
	    	$("#buyProduct h3").html(name);
			$('#buyProduct').popup('open');
			$("#numerror").html("");
        }
        
	    function checkNum(){
	    	var num = $("#buyNum").val();
	    	if(num <= 0){
	    		$("#numerror").html("购买数量必须大于0");
	    		return;
	    	}
	    	jQuery.ajax({  
		          type : 'GET',  
		          url : '${ctx}/expert/customerSolutionAndProduct/checkNum?pId='+buyProductId,  
		          success : function(data) {
			          var jsonObjects=JSON.parse(data);
			          if(num <= jsonObjects.lastNum){
			            $("#numerror").html("");
			            putCash();
			          } else {
			         	 $("#numerror").html("库存数量不足");
			          }
		          },  
		          error : function(data) {  
		        	  showTip("库存数量不足");
		          }  
		    }); 
	    }
	    
	    function putCash(){
	    	var customerId = '${customerId}';
	    	var num = $("#buyNum").val();
	    	var employeeId = $("#employeeId").val();
	    	$('#buyProduct').popup('close');
	    	jQuery.ajax({  
		          type : 'POST',  
		          url : '${ctx}/expert/customerSolutionAndProduct/add',  
		          data :{'csId': '${customerSolutionId}', 'pId':buyProductId, 'num': num, 'customerId': customerId, 'employeeId': employeeId},
		          success : function(data) {
		        	  showTip("添加成功");
		          },  
		          error : function(data) {  
		        	  showTip("添加失败");
		          }  
		    }); 
	    	$("#buyNum").val("0");
	    }
	</script>
</head>
<body>
	<div class="icon-buttons">
		<c:if test="${target == 'buy'}">
			<a data-role="button" data-inline="true" data-icon="back" data-iconpos="notext"  href="${ctx}/expert/customerSolution/manager?customerExamId=${customerExamId}">返回</a>
			<a data-role="button" data-inline="true" data-icon="check" data-iconpos="notext" href="${ctx}/cash/member/detail?id=${customerId}">去购物车付款</a>
		</c:if>
		<c:if test="${target == 'detail'}">
			<a data-role="button" data-inline="true" data-icon="back" data-iconpos="notext"  href="${ctx}/expert/customerSolution/list?customerExamId=${customerExamId}">返回</a>
		</c:if>
	</div>
	<c:if test="${target == 'buy'}">
	<nav data-role="navbar">
		<ul>
			<li><a class="ui-btn-active" href="${ctx}/expert/customerSolutionAndProduct/detail?customerSolutionId=${customerSolutionId}&target=buy">产品续买</a></li>
			<li><a href="${ctx}/expert/customerSolutionAndCare/detail?customerSolutionId=${customerSolutionId}&target=buy">护理续买</a></li>
		</ul>
	</nav>
	<br/>
	</c:if>
	<table id="contentTable" class="display">
		<thead><tr><th>产品名称</th><th>产品单价</th>
			<th>到期时间</th>
			<c:if test="${target == 'buy'}">
				<th>操作</th>
			</c:if>
		</tr></thead>
		<tbody>
		<c:forEach items="${list}" var="customerSolutionAndProduct">
			<tr id="${customerSolutionAndProduct.storeProduct.id}">
				<c:if test="${customerSolutionAndProduct.storeProduct.product_type == 0}">
					<td class="name">${customerSolutionAndProduct.storeProduct.product.name}</td>
					<td>${customerSolutionAndProduct.storeProduct.product.retail_price}</td>
				</c:if>
				<c:if test="${customerSolutionAndProduct.storeProduct.product_type == 1}">
					<td class="name">${customerSolutionAndProduct.storeProduct.storeSelfProduct.name}</td>
					<td>${customerSolutionAndProduct.storeProduct.storeSelfProduct.retail_price}</td>
				</c:if>
				<td>${fns:formatDate(customerSolutionAndProduct.notifyDate, "yyyy-MM-dd")}</td>
				<c:if test="${target == 'buy'}">
					<td>
					   	<a data-role="button" data-inline="true" href="" onclick="buyProduct('${customerSolutionAndProduct.storeProduct.id}')">续买</a>
					</td>
				</c:if>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<div data-role="popup" id="buyProduct" class="ui-corner-all">
		<form style="padding:20px;width:300px;">
			<label style="display:none;color:red;text-align:center;width:100%;"></label>
			<div class="ui-field-contain"><h3></h3></div>
			<div class="ui-field-contain">
				<label for="buyNum">产品数量：</label><input type="number" id="buyNum" placeholder="请输入产品数量" value="0">
				<label style="color:red" id="numerror"></label>
			</div>
				
			<div class="ui-field-contain">
				<label for="employeeId">服务专员：</label>
				<select id="employeeId" name="employeeId">   
				  	<c:forEach var="map" items="${fns:getEmployee()}">
					 	<option value='${map.key}'>  
					  		${map.value}  
						</option>   
				 	</c:forEach>
				</select> 
			</div>
			<input type="button" data-icon="check" onclick="checkNum()" value="加入购物车">
		</form>
	</div>
</body>
</html>
